﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Adapter.SAP;
namespace SAPInvoker.Controls
{
    public partial class TableInput : UserControl
    {
        private DataSet _DataSet;
        public TableInput()
        {
            InitializeComponent();
            this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
        }

        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != this.treeView1.Nodes[0])
            {
                string name = e.Node.Name;
                if (!string.IsNullOrEmpty(currentDisplayGridName))
                {
                    this.panelGridView.Controls[currentDisplayGridName].Visible = false;
                }
                currentDisplayGridName = "grid_" + name;
                this.panelGridView.Controls[currentDisplayGridName].Visible = true;
            }
        }
        string currentDisplayGridName = "";
        public void BindData(DataSet ds)
        {
            this._DataSet = ds;
            foreach (DataTable dt in ds.Tables)
            {
                string name = dt.TableName;
                TreeNode node = new TreeNode(name);
                node.Name = name;
                this.treeView1.Nodes[0].Nodes.Add(node);
                DataGridView grid = new DataGridView();
                grid.AutoGenerateColumns = true;
                grid.AllowUserToAddRows = true;
                grid.AllowUserToDeleteRows = true;
                grid.EditMode = DataGridViewEditMode.EditOnEnter;
                grid.Visible = false;
                grid.Name = "grid_" + name;
                grid.Height = this.panelGridView.Height - 10;
                grid.Width = this.panelGridView.Width - 10;
                grid.DataSource = dt;
                this.panelGridView.Controls.Add(grid);

            }
        }

        private TableParamsInfo TableParamsProvider()
        {
            string sql = "";
            int i=0;
            List<SAPParameter> listParams = new List<SAPParameter>();
            foreach (DataTable dt in this._DataSet.Tables)
            {
                i++;
                string name=dt.TableName;
                string tmpSql = string.Format("@{0}={1} OUTPUT",name,"@p"+i);
                sql = sql + tmpSql + ",";
                SAPParameter p = new SAPParameter("@p" + i, ParameterDirection.InputOutput);
                DataGridView grid = this.panelGridView.Controls["grid_" + name] as DataGridView;
                if (grid != null)
                {
                    DataTable data = grid.DataSource as DataTable;
                    if (data.Rows.Count == 0)
                    {
                        DataRow dr = data.NewRow();
                        data.Rows.Add(dr);
                    }
                    p.Value = data;
                }
                listParams.Add(p);
            }
            TableParamsInfo pInfo = new TableParamsInfo(sql.TrimEnd(",".ToCharArray()), listParams);
            return pInfo;

        }

        public TableParamsInfo ParamsInfo
        {
            get { return TableParamsProvider(); }
        }
    }

    public class TableParamsInfo 
    {
        private string _sqlString;
        private List<SAPParameter> _sapParameters;

        public TableParamsInfo(string sqlString,List<SAPParameter> sapParameters)
        {
            this._sqlString = sqlString;
            this._sapParameters = sapParameters;
        }

        public string SqlString
        {
            get { return this._sqlString; }
        }
        public List<SAPParameter> SAPParameters
        {
            get { return this._sapParameters; }
        }

    }
}
